﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Beeflys.Generator
{
    sealed class ProtocolReqPackGenerator : IGenerator
    {
        public string Name => throw new NotImplementedException();

        public string ProtocolId { get; private set; }
        public string MethodFullName { get; private set; }


        public string ReqPackName { get; private set; }

        private StringBuilder sb = new StringBuilder();

        public void Start(UMethodInfo method)
        {
            this.MethodFullName = method.FullName;
            this.ProtocolId = method.GetProtocolId();

            this.GenerateRequest(method);

        }

        private void GenerateRequest(UMethodInfo method)
        {
            this.ReqPackName = method.GetReqPackName();
            if (string.IsNullOrEmpty(this.ReqPackName))
            {
                return;
            }

            var args = method.GetArgsTypes();

            sb.AppendLine("[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]");
            sb.AppendLine($"sealed class {this.ReqPackName}");
            sb.AppendLine("{");

            for (int i = 0; i < args.Count; i++)
            {
                var arg = args[i];
                string fullName = arg.FullName;
                if (arg.IsBaseTypeKeyword())
                {
                    fullName = arg.GetTypeFullNameFromKeyword();
                }

                sb.AppendLine($"public {fullName} item{i}");
                sb.AppendLine("{");
                sb.AppendLine("get;");
                sb.AppendLine("set;");
                sb.AppendLine("}");
            }

            sb.AppendLine("}");
        }

        private void GenerateResponse(UMethodInfo method)
        {
            //if (method.ReturnVoid)
            //{
            //    return;
            //}

            //this.ResPackName = this.ProtocolId + "_REQ_PACK";
            //sb.AppendLine("[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]");
            //sb.AppendLine($"sealed class {this.ResPackName}");
            //sb.AppendLine("{");

            //var returnType = method.GetReturnType();
            





            //sb.AppendLine("}");
        }


        public string Build()
        {
            return sb.ToString();
        }
    }
}
